/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gob.pnp.servlets;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import pe.gob.pnp.factories.*;

/**
 *
 * @author Home
 */
public class ReporteUsuarioServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/pdf");
        ServletOutputStream out = response.getOutputStream();
        Connection conn = null;
        try {
            String rutareportes = getServletContext().getRealPath("/WEB-INF/reportes");
            File reporte = new File(rutareportes + "/rtpListaUsuarios.jasper");
            FileInputStream in = new FileInputStream(reporte);
            conn = ConnectionPool.obtenerConexion();
            byte[] bytes = JasperRunManager.runReportToPdf(in, null, conn);
            out.write(bytes, 0, bytes.length);
            out.flush();
            out.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new ServletException(ex);
        } catch (JRException ex) {
            ex.printStackTrace();
            throw new ServletException(ex);
        } finally {
            out.close();
            ConnectionPool.liberarConexion(conn);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
